package com.springboot.cloud.sysadmin.organization.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.sysadmin.organization.dao.NoticeMapper;
import com.springboot.cloud.sysadmin.organization.entity.param.NoticeQueryParam;
import com.springboot.cloud.sysadmin.organization.entity.po.Notice;
import com.springboot.cloud.sysadmin.organization.service.INoticeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by ZengJian on 2021/2/3 0003 上午 9:22
 */

@Service
@Slf4j
public class NoticeService extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Override
    public boolean add(Notice notice) {
        return this.saveOrUpdate(notice);
    }

    @Override
    public boolean delete(Set<String> ids) {

        return this.removeByIds(ids);
    }

    @Override
    public boolean update(Notice notice) {
        Notice notice1 = this.getById(notice.getId());
        if (ObjectUtil.isNull(notice1) || notice1.getId().equals(notice.getId()) && get(notice.getId())) {
            return false;
        }
        return this.saveOrUpdate(notice);
    }

    @Override
    public Notice getNotice(String id) {
        return this.getById(id);
    }

    @Override
    public boolean get(String id) {
        Notice notice = this.getById(id);
        if (ObjectUtil.isNull(notice)) {
            return false;
        }
        return true;
    }

//    @Override
//    public List<Notice> getByDictType(String dictType){
//        return this.list(new QueryWrapper<Notice>().eq("dict_type",dictType).orderByAsc("dict_sort"));
//    }

    @Override
    public IPage<Notice> query(Page page, NoticeQueryParam noticeQueryParam) {
        SimpleDateFormat convert = new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<Notice> queryWrapper = noticeQueryParam.build();
        queryWrapper.like(StrUtil.isNotBlank(noticeQueryParam.getTitle()), "title", noticeQueryParam.getTitle());
        queryWrapper.like(StrUtil.isNotBlank(noticeQueryParam.getContent()), "content", noticeQueryParam.getContent());
        queryWrapper.like(StrUtil.isNotBlank(noticeQueryParam.getType()), "type", noticeQueryParam.getType());
        queryWrapper.eq(StrUtil.isNotBlank(noticeQueryParam.getStatus()), "status", noticeQueryParam.getStatus());
        if (noticeQueryParam.getCreatedTimeStart() != null && noticeQueryParam.getCreatedTimeEnd() != null) {
            queryWrapper.apply("date_format(created_time,'%Y-%m-%d') >= date_format('" + convert.format(noticeQueryParam.getCreatedTimeStart()) + "','%Y-%m-%d')");
            queryWrapper.apply("date_format(created_time,'%Y-%m-%d') <= date_format('" + convert.format(noticeQueryParam.getCreatedTimeEnd()) + "','%Y-%m-%d')");
        }
        return this.page(page, queryWrapper);
    }

    @Override
    public List<Map<String, Object>> getAll() {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Notice::getId, Notice::getTitle);
        return this.listMaps(queryWrapper);
    }
}
